<template>
	<view class="live-item u-border-bottom" @click="handleClick">
		<image class="cover-img" mode="aspectFill" :src="cover"></image>
		<text class="title u-line-1">{{title}}</text>
		<view class="info">
			<view class="author">
				<image class="avatar" mode="scaleToFill" width="58rpx" height="58rpx" :src="exhibitorLogo">
					</u-image>
					<text class="author-name u-line-1">{{exhibitor}}</text>
			</view>
			<view class="datetime">
				<u-image mode="scaleToFill" width="38rpx" height="38rpx" src="/static/imgs/live-date.png"></u-image>
				<text>{{formatStartTime}}</text>
			</view>
			<view class="views">
				<text>点击量：</text>
				<text>{{views | brieflyViews}}</text>
			</view>
		</view>
		<video class="live-video" id="liveVideo" v-if="isPlay" :src="videoSrc" controls autoplay @fullscreenchange="handleFullScreen" :show-fullscreen-btn="false"></video>
	</view>
</template>

<script>
	import dayjs from 'dayjs'
	export default {
		name: "LiveItem",
		props: {
			videoId: {
				type: Number
			},
			
			title: {
				type: String
			},

			cover: {
				type: String
			},

			exhibitor: {
				type: String
			},

			exhibitorLogo: {
				type: String
			},

			startTime: {
				type: String
			},

			endTime: {
				type: String
			},

			src: {
				type: String
			},
			
			views: {
				type: Number
			}
			
		},
		filters: {
			// 缩写视频点击量
			brieflyViews(views) {
				let str = String(views)
				if(str.length >= 4) {
					let arr = str.split('')
					let hundredth = arr[arr.length-3]
					
					if(hundredth != 0) {
						return str.slice(0, -3) + '.' + hundredth + 'k'
					}
					
					return str.slice(0, -3) + 'k'
				}
				
				return views
			}
		},
		data() {
			return {
				isPlay: false,
				videoSrc: '',
				ctx: null
			}
		},
		computed: {
			formatStartTime() {
				return dayjs(this.startTime).format('MM/DD HH:mm:ss')
			}
		},
		methods: {
			handleClick() {
				this.$emit('play', this.videoId)
				this.isPlay = true
				this.ctx = uni.createVideoContext('liveVideo', this)
				this.videoSrc = this.src
				this.ctx.requestFullScreen()
			},
			
			// 视频全屏事件
			handleFullScreen(e) {
				if(e.detail.fullScreen) {
					this.isPlay = true
				}else {
					this.ctx?.stop()
					this.ctx = null
					this.isPlay = false
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.live-item {
		margin-top: 38rpx;
		width: 100%;
		border-top-left-radius: 5px;
		border-top-right-radius: 5px;

		.cover-img {
			width: 100%;
			height: 282rpx;
			border-radius: 5px;
			box-shadow: 0 3px 6px 0 rgba($color: #000000, $alpha: .16);
		}

		.title {
			display: inline-block;
			width: 100%;
			box-sizing: border-box;
			font-size: 18px;
			font-family: '微软雅黑';
			font-weight: bold;
			padding: 32rpx 12rpx 0;
		}

		.info {
			width: 100%;
			box-sizing: border-box;
			padding: 20rpx 12rpx 12rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.author {
				display: flex;
				align-items: center;

				.avatar {
					width: 58rpx;
					height: 58rpx;
					border-radius: 5px;
					box-shadow: 0 3px 6px 0 rgba($color: #000000, $alpha: .16);
				}

				.author-name {
					margin-left: 22rpx;
					display: inline-block;
					width: 136rpx;
					box-sizing: border-box;
					font-size: 28rpx;
					color: #7D7D7D;
				}
			}

			.datetime {
				// margin-left: 30rpx;
				// flex: 1;
				display: flex;
				align-items: center;

				text {
					margin-left: 8rpx;
					font-size: 28rpx;
					color: #273367;
				}
			}
			
			.views{
				font-size: 28rpx;
				color: #7D7D7D;
				text:nth-child(1) {
					
				}
			}

			
		}
	
		.live-video{
			height: 100px;
			position: fixed;
			top: -100px;
			left: 0;
		}
	}
</style>
